local kuangkui = fk.CreateSkill{
  name = "swis__kuangkui",
}

local SeabornUtil = require "packages/seaborn/utility/utility"

local zhisuan_mark = "@swis__zhisuan"
local suits = { "log_diamond", "log_club", "log_heart", "log_spade", }

Fk:loadTranslationTable{
  ["swis__kuangkui"] = "匡揆",
  [":swis__kuangkui"] = "一名角色的回合结束时，若你本回合使用过【桃】，你可以令一名角色将手牌数调整至〖智算〗当前项的序数。",

  ["#swis__kuangkui-choose"] = "匡揆：可以令一名角色将手牌数调整至%arg"
}

kuangkui:addEffect(fk.TurnEnd, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(kuangkui.name) and player:hasSkill("swis__zhisuan") then
      return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        return e.data.card.trueName == "peach"
      end, Player.HistoryTurn) > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local suit = player:getMark(zhisuan_mark)
    if type(suit) ~= "string" then return end
    local n = table.indexOf(suits, suit)
    local targets = table.filter(room:getAlivePlayers(false), function (p)
      return p:getHandcardNum() ~= n
    end)
    if #targets == 0 then return end
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      cancelable = true,
      prompt = "#swis__kuangkui-choose:::"..n,
      skill_name = kuangkui.name,
    })
    if #tos > 0 then
      event:setCostData(self, {tos[1], n})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local to, n = table.unpack(event:getCostData(self)) ---@type ServerPlayer, number
    SeabornUtil.adjustHand(to, n, kuangkui.name)
  end,
})

return kuangkui